
//
//  SHMyInviteNotesController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/6/12.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#import "SHMyInviteNotesController.h"
#import "SHMyInviteNotesModel.h"
#import "SHUserInviteNotesCell.h"
@interface SHMyInviteNotesController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myinviterTableView;
@property(nonatomic,strong)NSMutableArray *myinviterDataArray;
@property(nonatomic,assign)int pageIndex;
@property(nonatomic,assign)int count;


@end

@implementation SHMyInviteNotesController
- (instancetype)init{
    
    if (self  = [super init]) {
        _pageIndex = 1;
        _count = 200;
    }
    return  self;
}
- (NSMutableArray *)myinviterDataArray{
    
    if (!_myinviterDataArray) {
        _myinviterDataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _myinviterDataArray;
}
static NSString * const shUserInviteNotesCellID = @"shUserInviteNotesCellID";
- (UITableView *)myinviterTableView{
    
    if (!_myinviterTableView) {
        _myinviterTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
        _myinviterTableView.delegate = self;
        _myinviterTableView.dataSource = self;
        _myinviterTableView.rowHeight = __I6Height(60);
        _myinviterTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_myinviterTableView];
        [_myinviterTableView registerClass:[SHUserInviteNotesCell class] forCellReuseIdentifier:shUserInviteNotesCellID];
    }
    
    return _myinviterTableView;
}

#pragma mark - Delegate  & DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.myinviterDataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SHUserInviteNotesCell *inviterNotesCell = [tableView dequeueReusableCellWithIdentifier:shUserInviteNotesCellID];
    inviterNotesCell.myInviterModel = self.myinviterDataArray[indexPath.row];
    
    
    return inviterNotesCell;
}

- (void)refreshMyTableView{
    
    self.myinviterTableView.mj_header = [MJRefreshSHNormalHeader DYHeaderWithRefreshingBlock:^{
        _pageIndex = 1;
        [self getUserInviteNotesIsRemove:YES];

    }];
    [self.myinviterTableView.mj_header beginRefreshing];

    
}
#pragma mark --- 显示总共邀请人数
- (void)getUserInviteNotesIsRemove:(BOOL )isRemove{
    
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [MyDataService requestAFURL:[NSString stringWithFormat:KGetInviters,_pageIndex,_count] httpMethod:@"GET" params:nil data:nil complection:^(id result) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if ([result[@"success"] isEqualToNumber:@(1)] ) {
                if (isRemove) {
                   
                [self.myinviterDataArray removeAllObjects];
                    
                NSMutableArray *dataArray   =   [SHMyInviteNotesModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
                    if (_userIdentutyType == SHUserIsMember) {
                        for (SHMyInviteNotesModel *model in dataArray) {
                            if (!model.is_agent) {
                                [self.myinviterDataArray addObject:model];
                            }
                        }

                       
                    }else{
                       
                            for (SHMyInviteNotesModel *model in dataArray) {
                                if (model.is_agent) {
                                    [self.myinviterDataArray addObject:model];
                                }
                            }
                           
                        
                        
                    }

                    
                
                }else{
                    
                    NSMutableArray *moreDataArray =  [SHMyInviteNotesModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
                    for (SHMyInviteNotesModel *user in moreDataArray) {
                        [self.myinviterDataArray addObject:user];
                    }
                    
                    
                    
                }
                
     
              
                
            }else{
                
                [self.view makeToast:result[@"data"] duration:2.f position:@"center"];
                
            }
            
            
            
            
            

            
            
            //通知主线程刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                //回调或者说是通知主线程刷新，
                
                [self.myinviterTableView reloadData];
                [self.myinviterTableView.mj_header endRefreshing];
                [self.myinviterTableView.mj_footer endRefreshing];
            }); 

        } failure:^(id erro) {
            //通知主线程刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                //回调或者说是通知主线程刷新，
                
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                
                [self.myinviterTableView.mj_header endRefreshing];
                [self.myinviterTableView.mj_footer endRefreshing];
            });
            
        }];
        
        
    
    

    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getUserInviteNotesIsRemove:YES];
    [self refreshMyTableView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
